;(function(Ext,metaData){
	Ext.Loader.setPath("FormFieldUtil",contextPath+"ui_tpl/FormFieldUtil.js");
	var refMetaMap=metaData["refMetaMap"],
	refEnumMap=metaData["refEnumMap"],
	mainMetaId=metaData['id'],
	mainMeta=refMetaMap[mainMetaId],
	protoMetaId=mainMeta["meta"],
	protoMeta=refMetaMap[protoMetaId],
	fields=mainMeta["fields"],
	attrs=mainMeta["attrs"],
	action=attrs['action']||{type:"rest",url:"/meta/record/"+metaData["id"]},
	modelName="Ext.model."+mainMeta["code"],
	modelFields=[],columns=[],validators={},
	typeMap={"1":"string","-5":"int","4":"int","12":"string","16":"boolean","91":"date","93":"date","97":"date"};
	Ext.iterate(fields,function(field,i){
		
		var type=field["type"],name=field["name"],f={name:name,type:typeMap[field["type"]]},
		col={dataIndex:name,text:field["text"],menuDisabled:true},
		valid={};
		switch(type){
		case -5:
		case 4:
			break;
		case 91:
			col["renderer"]=Ext.util.Format.dateRenderer('Y-m-d');
			break;
		case 92:
			col["renderer"]=Ext.util.Format.dateRenderer('h:i:s');
			break;
		case 93:
			col["renderer"]=Ext.util.Format.dateRenderer('Y-m-d h:i:s');
			col["width"]=150;
			break;
		}
		modelFields.push(f);
		columns.push(col);
	});
	var startParam ="pageInfo.start",limitParam="pageInfo.limit",pageParam="pageInfo.page",
	sortParam="sortInfo.field",directionParam="sortInfo.direct",
	paramNames={
			startParam:startParam,
			limitParam:limitParam,
			pageParam:pageParam,
			sortParam:sortParam,
			directionParam:directionParam
			},extraParams={};
	extraParams[startParam]= 0;
	extraParams[limitParam]=25;
	extraParams[sortParam]=(attrs.idProperty||"id");
	extraParams[directionParam]="ASC";
	var proxy={
			url:action["url"],
			type:action["type"],
			extraParams: extraParams,
			reader: {  
                type: 'json',
                rootProperty: 'itemList',
                totalProperty: 'pageInfo.total'
            },
            simpleSortMode: true
		};
	Ext.apply(proxy,paramNames);
	Ext.define(modelName,{
		extend:"Ext.data.Model",
		idProperty:attrs["idProperty"]||"id",
		fields:modelFields,
		proxy:proxy
	});
	
	Ext.define("Ext.ux.GridForm",{
		extend:"Ext.container.Container",
		requires: [
	        'Ext.toolbar.Paging',
	        'FormFieldUtil',
	    ],
	    layout:"border",
		initComponent:function(){
			var me=this;
			me.metaData=metaData;
			Ext.apply(this,attrs);
			var dataGrid=this.dataGrid=this.initDataGrid('center'),
			detailForm=this.detailForm=this.initForm('east');
			this.items=[];
			this.items.push(dataGrid);
			this.items.push(detailForm);
			this.callParent();
		},
		refreshGrid:function(){
			this.store.reload();
		},
		initGridStore:function(){
			var me=this,store=this.store=Ext.create('Ext.data.Store',{
				model:modelName,
				autoLoad:true
			});
			return store;
		},
		
		initTBar:function(){
			var me=this,tbar=this.tbar=[{
				text:"刷新",
				handler:function(){
					me.refreshGrid();
				}
			}];
		},
		initDataGrid:function(region){
			var me=this;this.initGridStore();me.initTBar();
			if(me.showColumns&&me.showColumn.length>0){
				Ext.iterate(columns,function(col,i){
					var name=col["dataIndex"];
					var exist=Ext.Array.contains(me.showColumns,name);
					col["hidden"]=!exist;
				});
			}
			
			var me=this,grid=this.gridPanel={
				xtype:"grid",
				minHeight:300,
				width:340,
				tbar:me["tbar"],
				width:'auto',
				region:region,
				split:true,
			    store:me.store,
			    columns:columns,
				height:'auto',
				bbar: {
			        xtype: 'pagingtoolbar',
			        displayInfo: true,
			        displayMsg: ' {0} - {1} 总： {2}',
			        emptyMsg: "无数据",
			        store:me.store
			    },
			    listeners:{
			    	'rowclick':function ( view, record, element, rowIndex, e, eOpts ) {
			    		var formId=me["detailFormId"],detailForm=Ext.getCmp(formId);
			    		detailForm.loadRecord(record);
			    	}
			    }
			};
			return grid;
		},
		initForm:function(region){
				var me=this,attrs=mainMeta["attrs"];
				me["detailFormId"]="detailForm"+me["metaData"]["id"];
				var update=this.update=attrs["update"]||action,
				items=FormFieldUtil.getFormFields(mainMetaId,me["metaData"]);
				
				var config=this.detailFormConfig={
					xtype:'form',
				    title: '详细信息',
				    bodyPadding: 15,
				    region:region,
				    width:600,
				    scrollable:true,
				    layout: 'form',
				    defaults: {
				        anchor: '100%'
				    },
				    id:me["detailFormId"],
				    defaultType: 'textfield',
				    items:items,
				    buttons: [{
				        text: '重置',
				        iconCls:"fa fa-refresh fa-fw",
				        handler: function() {
				            this.up('form').getForm().reset();
				        }
				    }, {
				        text: '提交',
				        iconCls:"fa fa-arrow-up",
				        formBind: true,
				        disabled: true,
				        handler: function() {
				            var form = this.up('form').getForm();
				            if (form.isValid()) {
				                form.submit({
				                    success: function(form, action) {
				                    	Ext.toast("提交成功",'提示', 't');
				                    	me.refreshGrid();
				                    	
				                    },
				                    failure: function(form, action) {
				                    	Ext.toast(action.result.message,'提示', 't');
				                    	me.refreshGrid();
				                    }
				                });
				            }
				        }
				    }]
				};
				if(update["type"]=="json"){
					config["jsonSubmit"]=true;
				}
				config["url"]=update["url"];
				return config;
		}
	});
	
	Ext.application({
	    name: 'MyApp',
	    mainView:'Ext.ux.GridForm',
	    launch: function () {
	    }
	});
})(Ext,metaData);